package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Role;
import cn.wolfcode.domain.Room;
import cn.wolfcode.domain.Student;
import cn.wolfcode.mapper.StudentMapper;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.StudentQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.service.RoomService;
import cn.wolfcode.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private RoomService roomService;


    @RequestMapping("/list")
    @RequiresPermissions("student:list")
    public String list(Model model,@ModelAttribute("qo") StudentQueryObject qo) {
        PageInfo<Student> pageInfo = studentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "student/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions("student:delete")
    public String deleteById(Long id) {
        studentService.deleteById(id);
        return "redirect:/student/list";
    }

    @RequestMapping("/input")
    @RequiresPermissions("student:input")
    public String input(Long id, Model model) {
        //遍历所有角色
        List<Role> roleList = roleService.listAll();
        model.addAttribute("roleList", roleList);

        //遍历所有宿舍
        List<Room> rooms = roomService.selAll();
        model.addAttribute("rooms", rooms);

        if (id != null) {
            //遍历当前用户信息
            Student student = studentService.selStudentById(id);
            model.addAttribute("student", student);
            //遍历当前用户角色信息
            List<Role> studentRoleList = roleService.selRolesByStudentId(id);
            model.addAttribute("studentRoleList", studentRoleList);
        }
        return "student/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions("student:saveOrUpdate")
    public JsonResult saveOrUpdate(Student student,Long[] roleIds,Long roomId) {
        student.setRoom(new Room());
        student.getRoom().setId(roomId);
        studentService.saveOrUpdate(student,roleIds);
        return new JsonResult(true, "保存成功");
    }

    @RequestMapping("/checkUsername")
    @ResponseBody
    @RequiresPermissions("student:checkUsername")
    public Map<String, Boolean> checkUsername(String username) {
        Boolean flag = studentService.checkUsername(username);
        Map<String, Boolean> map = new HashMap<>();
        map.put("valid", flag);
        return map;
    }

    @RequestMapping("/checkRoomPerson")
    @ResponseBody
    public Map<String,Boolean> checkRoomPerson(Long roomId) {
        Boolean flag = studentService.checkRoomPerson(roomId);
        Map<String, Boolean> map = new HashMap<>();
        map.put("valid", flag);
        return map;
    }
}
